﻿<?php
function DoCommandSysConfig($command_type)
{
	switch ($command_type) {
		case "update_login_prompt":
			SysConfig_Update_Login_Prompt();
			break;

		default:
			breake;
	}
}

function DoCommandUpload($command_type)
{
	switch ($command_type) {
		case "furniture":
			HttpAction_UploadFurniture();
			break;
		case "photo":
			HttpAction_UploadUserPhoto();
			break;
		case "headphoto":
			HttpAction_UploadUserHeadPhoto();
			break;
		case "glasses":
			HttpAction_UploadGlasses();
			break;
		case "hat":
			HttpAction_UploadHat();
			break;
		case "hair":
			HttpAction_UploadHair();
			break;
		case "face":
			HttpAction_UploadFace();
			break;
		case "skin":
			HttpAction_UploadSkin();
			break;
		case "cost":
			HttpAction_UploadCost();
			break;
		case "trousers":
			HttpAction_UploadTrousers();
			break;
		case "skirt":
			HttpAction_UploadSkirt();
			break;
		case "socks":
			HttpAction_UploadSocks();
			break;
		case "shoes":
			HttpAction_UploadShoes();
			break;
		case "bag":
			HttpAction_UploadBag();
			break;
		case "necklace":
			HttpAction_UploadNecklace();
			break;
		case "earring":
			HttpAction_UploadEarring();
			break;
		case "bracelet":
			HttpAction_UploadBracelet();
			break;
		case "wing":
			HttpAction_UploadWing();
			break;
			
		case "tattoo":
			HttpAction_UploadTattoo();
			break;
		case "barwaiter":
			HttpAction_UploadBarwaiter();
			break;
		case "music":
			HttpAction_UploadMusic();
			break;
		case "present":
			HttpAction_UpdatePresent();
			break;
			
		case "dress_packet":
			HttpAction_UpdateDressPacket();
			break;

		case "amulet":
			HttpAction_UploadAmulet();
			break;
		default:
			breake;
	}
}

function DoCommandInfo($command_type)
{
	switch ($command_type) {
		case "glasses_subtype_list":
			HttpAction_GetGlassesSubTypeList();
			break;
		case "hat_subtype_list":
			HttpAction_GetHatSubTypeList();
			break;
		case "hair_subtype_list":
			HttpAction_GetHairSubTypeList();
			break;
		case "face_subtype_list":
			HttpAction_GetFaceSubTypeList();
			break;
		case "skin_subtype_list":
			HttpAction_GetSkinSubTypeList();
			break;
		case "cost_subtype_list":
			HttpAction_GetCostSubTypeList();
			break;
		case "trousers_subtype_list":
			HttpAction_GetTrousersSubTypeList();
			break;
		case "skirt_subtype_list":
			HttpAction_GetSkirtSubTypeList();
			break;
		case "bag_subtype_list":
			HttpAction_GetBagSubTypeList();
			break;
		case "necklace_subtype_list":
			HttpAction_GetNecklaceSubTypeList();
			break;

		case "earring_subtype_list":
			HttpAction_GetEarringSubTypeList();
			break;
		case "bracelet_subtype_list":
			HttpAction_GetBraceletSubTypeList();
			break;
		case "wing_subtype_list":
			HttpAction_GetWingSubTypeList();
			break;
		
		case "tattoo_subtype_list":
			HttpAction_GetTattooSubTypeList();
			break;
		case "barwaiter_subtype_list":
			HttpAction_GetBarwaiterSubTypeList();
			break;
		case "music_subtype_list":
			HttpAction_GetMusicSubTypeList();
			break;
		case "shoes_subtype_list":
			HttpAction_GetShoesSubTypeList();
			break;
		case "socks_subtype_list":
			HttpAction_GetSocksSubTypeList();
			break;
		case "dress_packet_subtype_list":
			HttpAction_GetDressPacketSubTypeList();
			break;
			
		case "present_subtype_list":
			HttpAction_GetPresentSubTypeList();
			break;
			
		case "furniture_subtype_list":
			HttpAction_GetFurnitureSubTypeList();
			break;
			
		case "jobs_award_type_list":
			HttpAction_GetJobsAwardTypeList();
			break;

		case "get_constellation_descript":
			HttpAction_GetConstellationDescript();
			break;
		case "update_constellation_descript":
			HttpAction_UpdateConstellationDescript();
			break;
		case "get_constellation_pair_descript":
			HttpAction_GetConstellationPairDescript();
			break;
		case "update_constellation_pair_descript":
			HttpAction_UpdateConstellationPairDescript();
			break;

		case "get_amulet":
			HttpAction_GetAmulet();
			break;
		case "delete_amulet":
			HttpAction_DeleteAmulet();
			break;

		case "get_beautiful":
			HttpAction_GetBeautiful();
			break;
		case "delete_beautiful":
			HttpAction_DeleteBeautiful();
			break;
						
		case "get_beautiful_present":
			HttpAction_GetBeautifulPresent();
			break;
		case "delete_beautiful_present":
			HttpAction_DeleteBeautifulPresent();
			break;
			
		case "get_beautiful_furniture":
			HttpAction_GetBeautifulFurniture();
			break;
		case "delete_beautiful_furniture":
			HttpAction_DeleteBeautifulFurniture();
			break;
						
		case "get_dress_packet":
			HttpAction_GetDressPacket();
			break;

		case "delete_dress_packet":
			HttpAction_DeleteDressPacket();
			break;

		case "update_jobs":
			HttpAction_UpdateJobs();
			break;
		case "get_jobs":
			HttpAction_GetJobs();
			break;
		case "delete_jobs":
			HttpAction_DeleteJobs();
			break;
			
		case "update_question":
			HttpAction_UpdateQuestion();
			break;
		case "get_question_group_list":
			HttpAction_GetQuestionGroupList();
			break;
		case "get_question":
			HttpAction_GetQuestion();
			break;
		case "get_question_option":
			HttpAction_GetQuestionOption();
			break;
		case "delete_question":
			HttpAction_DeleteQuestion();
			break;

		default:
			break;
	}
}

function DoCommandPagind($command_type)
{
	switch ($command_type) {
		case "select_total_count": // 通用分页查询
			HttpAction_GetTotalCount();
			break;
		case "query_paging": // 通用分页查询
			HttpAction_QueryPaging();
			break;
			
		case "get_beautiful_total": // 装扮
			HttpAction_GetBeautifulTotal();
			break;
		case "get_beautiful_list": // 装扮
			HttpAction_GetBeautifulList();
			break;
			
		case "get_beautiful_present_total": // 礼品
			HttpAction_GetBeautifulPresentTotal();
			break;
		case "get_beautiful_present_list": // 礼品
			HttpAction_GetBeautifulPresentList();
			break;
			
		case "get_beautiful_furniture_total": // 家具
			HttpAction_GetBeautifulFurnitureTotal();
			break;
		case "get_beautiful_furniture_list": // 家具
			HttpAction_GetBeautifulFurnitureList();
			break;

		case "get_dress_packet_total": //套装
			HttpAction_GetDressPacketTotal();
			break;
		case "get_dress_packet_list":
			HttpAction_GetDressPacketList();
			break;

		case "get_question_total":  //问题
			HttpAction_GetQuestionTotal();
			break;
		case "get_question_list":
			HttpAction_GetQuestionList();
			break;

		default:
			break;
	}
}
// --------------------------分页---------
// 查询总行数
function HttpAction_GetTotalCount()
{
	$table_name = GetRequest("table_name");

	$str_sql = sprintf("select count(*) as total_count from %s",
	    EscapeString($table_name));

	DBHelper::QueryReturnJSON($str_sql);
}
// 查询分页数据
function HttpAction_QueryPaging()
{
	$table_name = GetRequest("table_name"); //表名
	$page_index = GetRequest("page_index"); //当前查询的页码
	$per_page_count = GetRequest("per_page_count"); //每页行数
	$select_parms = GetRequest("select_parms"); //
	$count = ($page_index - 1) * $per_page_count;

	$str_sql = sprintf("select %s from %s  limit %s,%s",
	    EscapeString($select_parms),
	    EscapeString($table_name),
	    $count,
	    EscapeString($per_page_count)
	    );

	DBHelper::QueryReturnJSON($str_sql);
}


//-------系统配置修改------用户登录提示---------
function SysConfig_Update_Login_Prompt()
{ 
	$prompt_value = $_REQUEST["prompt_value"];
    
	$str_sql = sprintf("update t_sys_config set c_value = '%s', c_time= now() where c_catalog= 'other' and c_name = 'login_prompt';",
	EscapeString($prompt_value));
    
    DBHelper::ExecAndPrintMsg($str_sql);
}


//-------图片上传---------------
function HttpAction_UploadUserPhoto()
{
	$session_id = $_REQUEST["session_id"];
	$str_sql = sprintf("select c_id from t_userinfo where c_session_id='%s'",
	EscapeString($session_id));

	$rArray = DBHelper::Query($str_sql, $msg);
	if($rArray == -1 || count($rArray) == 0)
	{
		die("fatal: session_id not exists. ". $msg);
	}

	$userid = $rArray[0]["c_id"];  //根据session_id获取用户id

	$fileInputName = "file";
	$files = $_FILES[$fileInputName];
	$errors = $files["error"];
	$name = $files['name'];

	if($errors == UPLOAD_ERR_NO_FILE )
	{
		die("fatal:no attachments");  //没有文件上传
	}

	if($errors != UPLOAD_ERR_OK)
	{
		die("fatal:upload attachments error"); //出错
	}

	//获取上传的文件的信息
	$name = $files['name'];
	$extname = GetFileExtName($name);

	$book_id = $userid;//放相片的文件夹

	$str_sql = sprintf("insert into t_photo(c_book_id, c_name, c_extname) values('%s', '%s', '%s')",
		EscapeString($book_id),
		EscapeString($name),
		EscapeString($extname)
		);

	$id =  DBHelper::InsertReturnID($str_sql, $msg);
	if($id == -1)
	{
		die("fatal: insert db error: ". $msg);
	}
  
	$user_zone = intval($userid / 10000);
	$newPath = GetUploadPath() . GetConfInfo("update_path_photo") . $user_zone. "/" . $userid . "/";
	$newfilename =  $id . "." . $extname;

	UploadFile($fileInputName, $newPath, $newfilename);

	echo "success:" . $id;  //返回
}
//传头像
function HttpAction_UploadUserHeadPhoto()
{
	try
	{		
	//---
	//$fptest=fopen("./upload_log",'w');  
	//---
	
		$session_id = $_REQUEST["session_id"];
		$user_name = "";
		
		$b_user_name = false;		
		
		if(isset($_REQUEST["user_name"]))
		{
			$user_name = $_REQUEST["user_name"];
			
			if(strlen($user_name) > 0)
				$b_user_name = true;
		}
		$str_sql = "";
		
		if($b_user_name == true)
		{
			$str_sql = sprintf("select c_id from t_userinfo where c_basic_username='%s'",
				EscapeString($user_name));
		}
		else
		{
			$str_sql = sprintf("select c_id from t_userinfo where c_session_id='%s'",
				EscapeString($session_id));
		}


//---
/*
fwrite($fptest, $session_id . "\n");
fwrite($fptest, $user_name . "\n");
fwrite($fptest, $str_sql . "\n");
*/
//---

		$rArray = DBHelper::Query($str_sql, $msg);
		if($rArray == -1 || count($rArray) == 0)
			throw new Exception("fatal: session_id not exists. ". $msg);

		$userid = $rArray[0]["c_id"];  //根据session_id获取用户id

		$img = file_get_contents("php://input");
		if (!$img)
			throw new Exception("not image data");

		//$newPath = GetUploadPath() . GetConfInfo("update_path_photo") . $userid . "/";
		//$newName = $userid . ".png";     //文件名字
		$user_zone = intval($userid / 10000);
		$newPath = GetUploadPath() . GetConfInfo("update_path_photo") . $user_zone. "/" . $userid . "/";
		$newName =  $userid . ".png";
		$file_dir = $newPath . $newName; //全路径

		CreateFolder($newPath); //建立文件夹
		$fp = fopen($file_dir,'w');
		fwrite($fp, $img);
		fclose($fp);
		
		//给用户 增加金币100
		//$str_sql = sprintf("update t_userinfo set c_game_gold = c_game_gold + 100 where c_session_id = '%s'",
		//EscapeString($session_id));
		
		if($b_user_name == true)
		{
			$str_sql = sprintf("call proc_upload_head_img2('%s', '%s') ",
				EscapeString($user_name), EscapeString($session_id));
		}
		else
		{
			$str_sql = sprintf("call proc_upload_head_img('%s') ",
				EscapeString($session_id));
		}
		
//---
/*
fwrite($fptest, $str_sql . "\n");
fclose($fptest); 
*/
//---

		DBHelper::Exec($str_sql, $msg); 		

		echo "success";  //返回
	}
	catch(Exception $e)
	{
		echo $e->getMessage();
	}
}

// ---编辑工作表------
function HttpAction_UpdateJobs()
{
	$type = $_REQUEST["type"];
	$oldType = $_REQUEST["old_type"];
	$name = $_REQUEST["name"];
	$award_type = $_REQUEST["award_type"];
	$award = $_REQUEST["award"];
	$memo = $_REQUEST["memo"];

	$str_sql = sprintf("select * from t_jobs where c_type = '%s'",
	    EscapeString($oldType));
	if (DBHelper::QueryIsExistRow($str_sql)) {
		// 存在 则修改数据
		$str_sql = sprintf("update t_jobs set c_type=%s, c_name='%s',
		c_award_type='%s',  c_award='%s',  c_memo='%s' where c_type=%s",
		EscapeString($type),
		EscapeString($name),
		EscapeString($award_type),
		EscapeString($award),
		EscapeString($memo),
		EscapeString($oldType));
	} else {
		// 插入主表
		$str_sql = sprintf("insert into t_jobs(c_type, c_name, c_award_type,
			c_award, c_memo) values('%s', '%s', '%s', '%s', '%s')",
		EscapeString($type),
		EscapeString($name),
		EscapeString($award_type),
		EscapeString($award),
		EscapeString($memo));
	}

	DBHelper::ExecAndPrintMsg($str_sql);
}
// 获取工作表一条记录
function HttpAction_GetJobs()
{
	$type = $_REQUEST["type"];
	$str_sql = sprintf("select *  from t_jobs where c_type = %s",
	    EscapeString($type));
	DBHelper::QueryReturnJSON($str_sql);
}
function HttpAction_DeleteJobs()
{
	$type = $_REQUEST["type"];

	$str_sql = sprintf("select * from t_jobs where c_type = %s", $type);
	$msg = "";
	if (!DBHelper::QueryIsExistRow($str_sql, $msg)) {
		ReturnJSONErrorMsg("数据不存在..." . $msg);
		return;
	}

	$str_sql = sprintf("delete from t_jobs where c_type = %s",
	    EscapeString($type));
	DBHelper::ExecReturnJSON($str_sql);
}

// --------装扮------------------
// 获取某种装扮的总数
function HttpAction_GetBeautifulTotal()
{
	$tableName = $_REQUEST["table"];
	$gender = $_REQUEST["gender"];
	$str_sql = sprintf("select count(*) as total_count from t_store_%s",
	    EscapeString($tableName));

	// 2表示两者
	if ($gender != 2)
		$str_sql = sprintf("%s where c_gender = '%s'", $str_sql, $gender);

	DBHelper::QueryReturnJSON($str_sql);
}
// 获取某种装扮的所有记录
function HttpAction_GetBeautifulList()
{
	$tableName = $_REQUEST["table"];
	$gender = $_REQUEST["gender"];

	$newPath = GetBeautifulPath($tableName); //存放文件的文件夹路径
	$page_index = GetRequest("page_index"); //当前查询的页码
	$per_page_count = GetRequest("per_page_count"); //每页行数
	$count = ($page_index - 1) * $per_page_count;

	$str_sql = sprintf("select *, '%s' as filepath from t_store_%s",
	    $newPath, EscapeString($tableName));

	if ($gender != 2) { // 2表示男女都查
		$str_sql = sprintf("%s where c_gender = '%s'", $str_sql, $gender);
	}

	$str_sql = sprintf("%s order by c_new desc limit %d,%d", $str_sql, $count, $per_page_count);

	DBHelper::QueryReturnJSON($str_sql);
}
// 获取某种装扮的一条记录
function HttpAction_GetBeautiful()
{
	$id = $_REQUEST["id"];
	$tableName = $_REQUEST["table"];

	$newPath = GetBeautifulPath($tableName); //存放文件的文件夹路径
	$str_sql = sprintf("select *, '%s' as filepath from t_store_%s where c_id = %s",
	    $newPath, EscapeString($tableName), EscapeString($id));

	DBHelper::QueryReturnJSON($str_sql);
}
// 删除一条装扮记录
function HttpAction_DeleteBeautiful()
{
	$id = $_REQUEST["id"];
	$tableName = $_REQUEST["table"];

	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);

	$msg = "";
	$rsArray = DBHelper::Query($str_sql, $msg);
	if ($rsArray == null) {
		ReturnJSONErrorMsg("错误：" . $msg);
		return;
	}
	if (count($rsArray) <= 0) {
		ReturnJSONErrorMsg("数据不存在...");
		return;
	}
	// 删除数据库
	$str_sql = sprintf("delete from t_store_%s where c_id = %s",
	    EscapeString($tableName),
	    EscapeString($id));

	if (DBHelper::ExecReturnJSON($str_sql)) {
		// 删除文件
		$gender = $rsArray[0]["c_gender"]; //性别信息
		$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件信息
		DeleteFileArray($fileInfoArray);
	}
}
// 上传装扮用品
function HttpAction_UpdateBeautiful($tableName)
{
	if ($tableName != "music")
	{
		CheckFileInfo("file_0");	
		CheckFileInfo("file_1");
	}

	if ($tableName == "shoes" || $tableName == "skin" || $tableName == "bag" || $tableName == "bracelet")
		CheckFileInfo("file_2");

	$id = $_REQUEST["id"];

	$gender = $_REQUEST["gender"];
	$level = $_REQUEST["level"];
	$c_new = $_REQUEST["c_new"];
	$sub_type = GetRequest("sub_type");

	$name = $_REQUEST["name"];
	$price_gold = $_REQUEST["price_gold"];
	$price_money = $_REQUEST["price_money"];

	$charm = $_REQUEST["charm"];
	$courage = $_REQUEST["courage"];
	$power = $_REQUEST["power"];
	$passion = $_REQUEST["passion"];

	$saleable = $_REQUEST["saleable"];
	$status = $_REQUEST["status"];
	$memo = $_REQUEST["memo"];

	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);

	$bExec = true;
	$msg = "";
	$rsArray = DBHelper::Query($str_sql, $msg);
	if ($rsArray == - 1) {
		PrintMsgToHTML("错误：" . $msg);
		return;
	}

	if (count($rsArray) > 0) {
		$genderOld = $rsArray[0]["c_gender"];
		if ($gender != $genderOld) { // 修改了性别, 修改文件名
			EditBeautifulFileName($tableName, $id, $genderOld, $id, $gender);
		}
		// 存在 则修改数据
		$str_sql = sprintf("update t_store_%s set c_saleable='%s',c_name='%s',
		c_price_gold='%s', c_price_money='%s', c_gender='%s', c_status='%s',
		c_memo='%s', c_sub_type='%s', c_charm='%s', c_courage='%s',c_power='%s',
		c_passion='%s', c_level='%s',c_new='%s' where c_id='%s'",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level),
		EscapeString($c_new),
		EscapeString($id)
		);

		if (DBHelper::Exec($str_sql, $msg) == - 1) {
			$bExec = false;
		}
	} else {
		// 不存在， insert
		$str_sql = sprintf("insert into t_store_%s(c_saleable,c_name,
			c_price_gold,c_price_money,c_gender,c_status,
			c_memo,c_sub_type,c_charm,c_courage,c_power,c_passion,c_level,c_new)
			values('%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level),
		EscapeString($c_new)
		);

		$id = DBHelper::InsertReturnID($str_sql, $msg);
		if ($id == - 1) {
			$bExec = false;
		}
		else
		{
			//头发 或 肤色  并赠送checkbox打钩， 表示赠送所有用户
			if(($tableName == "hair" || $tableName == "skin") && isset($_REQUEST["ck_gift_beautiful"]))
			{
				$str_sql = sprintf("insert into t_my_%s(c_%s_id, c_owner) select %d, c_id from t_userinfo where c_basic_gender=%s ",
				$tableName, $tableName, $id, $gender);

				if(DBHelper::Exec($str_sql, $msg) == -1)
					$bExec = false;
			}
		}
	}

	if (!$bExec) {
		PrintMsgToHTML("编辑失败: " . $msg);
		return;
	}
	$finfo_0 = "";
	$finfo_1 = "";
	$finfo_2 = "";
	
	$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件路径信息
	$finfo_0 = UploadFile("file_0", $fileInfoArray[0]["path"], $fileInfoArray[0]["name"]);
	
	if($tableName != "music") 
		$finfo_1 = UploadFile("file_1", $fileInfoArray[1]["path"], $fileInfoArray[1]["name"]);
  
	if ($tableName == "shoes" || $tableName == "skin" || $tableName == "bag" || $tableName == "bracelet")
		$finfo_2 = UploadFile("file_2", $fileInfoArray[2]["path"], $fileInfoArray[2]["name"]);

	PrintMsgToHTML("编辑成功", $finfo_0, $finfo_1, $finfo_2);
}

// 眼镜
function HttpAction_UploadGlasses()
{
	HttpAction_UpdateBeautiful("glasses");
}
// 帽子
function HttpAction_UploadHat()
{
	HttpAction_UpdateBeautiful("hat");
}
// 头发
function HttpAction_UploadHair()
{
	HttpAction_UpdateBeautiful("hair");
}
// 脸型
function HttpAction_UploadFace()
{
	HttpAction_UpdateBeautiful("face");
}
// 肤色
function HttpAction_UploadSkin()
{
	HttpAction_UpdateBeautiful("skin");
}
// 上衣
function HttpAction_UploadCost()
{
	HttpAction_UpdateBeautiful("cost");
}
// 裤子
function HttpAction_UploadTrousers()
{
	HttpAction_UpdateBeautiful("trousers");
}
// 连衣裙
function HttpAction_UploadSkirt()
{
	HttpAction_UpdateBeautiful("skirt");
}
// 袜子
function HttpAction_UploadSocks()
{
	HttpAction_UpdateBeautiful("socks");
}
// 鞋子
function HttpAction_UploadShoes()
{
	HttpAction_UpdateBeautiful("shoes");
}
// 提包
function HttpAction_UploadBag()
{
	HttpAction_UpdateBeautiful("bag");
}
// 项链
function HttpAction_UploadNecklace()
{
	HttpAction_UpdateBeautiful("necklace");
}
// 耳环
function HttpAction_UploadEarring()
{
	HttpAction_UpdateBeautiful("earring");
}
// 手链
function HttpAction_UploadBracelet()
{
	HttpAction_UpdateBeautiful("bracelet");
}
//
function HttpAction_UploadWing()
{
	HttpAction_UpdateBeautiful("wing");
}
function HttpAction_UploadTattoo()
{
	HttpAction_UpdateBeautiful("tattoo");
}
function HttpAction_UploadBarwaiter()
{
	HttpAction_UpdateBeautiful("barwaiter");
}
function HttpAction_UploadMusic()
{
	HttpAction_UpdateBeautiful("music");
}
//----- 礼品上传
function HttpAction_UpdatePresent()
{
	CheckFileInfo("file_0");

	$tableName = "present";
	$id = $_REQUEST["id"];

	$gender = $_REQUEST["gender"];
	$gender = "0";  //礼品的性别默认是女
	
	$level = $_REQUEST["level"];
	$sub_type = GetRequest("sub_type");

	$name = $_REQUEST["name"];
	$price_gold = $_REQUEST["price_gold"];
	$price_money = $_REQUEST["price_money"];

	$charm = $_REQUEST["charm"];
	$courage = $_REQUEST["courage"];
	$power = $_REQUEST["power"];
	$passion = $_REQUEST["passion"];

	$saleable = $_REQUEST["saleable"];
	$status = $_REQUEST["status"];
	$memo = $_REQUEST["memo"];

	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);

	$bExec = true;
	$msg = "";
	$rsArray = DBHelper::Query($str_sql, $msg);
	if ($rsArray == - 1) {
		PrintMsgToHTML("错误：" . $msg);
		return;
	}

	if (count($rsArray) > 0) {
		// 存在 则修改数据
		$str_sql = sprintf("update t_store_%s set c_saleable='%s',c_name='%s',
		c_price_gold='%s', c_price_money='%s', c_gender='%s', c_status='%s',
		c_memo='%s', c_sub_type='%s', c_charm='%s', c_courage='%s',c_power='%s',
		c_passion='%s', c_level='%s' where c_id='%s'",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level),
		EscapeString($id)
		);

		if (DBHelper::Exec($str_sql, $msg) == - 1) {
			$bExec = false;
		}
	} else {
		// 不存在， insert
		$str_sql = sprintf("insert into t_store_%s(c_saleable,c_name,
			c_price_gold,c_price_money,c_gender,c_status,
			c_memo,c_sub_type,c_charm,c_courage,c_power,c_passion,c_level)
			values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level)
		);

		$id = DBHelper::InsertReturnID($str_sql, $msg);
		if ($id == - 1) {
			$bExec = false;
		}
	}

	if (!$bExec) {
		PrintMsgToHTML("编辑失败: " . $msg);
		return;
	}

	$gender = "0";  //礼品的文件全默认性别为0
	$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件路径信息
	$finfo_0 = UploadFile("file_0", $fileInfoArray[0]["path"], $fileInfoArray[0]["name"]);
   
	PrintMsgToHTML("编辑成功", $finfo_0);
}


//--- 获取某种礼品的总数(查询使用)
function HttpAction_GetBeautifulPresentTotal()
{
	$tableName = "present";
	$type = $_REQUEST["type"];
	$str_sql = sprintf("select count(*) as total_count from t_store_%s where c_sub_type = '%s' ", $tableName, EscapeString($type));

	DBHelper::QueryReturnJSON($str_sql);
}
// 获取某种礼品的所有记录(查询使用)
function HttpAction_GetBeautifulPresentList()
{
	$tableName = "present";
	$type = $_REQUEST["type"];

	$newPath = GetBeautifulPath($tableName); //存放文件的文件夹路径
	$page_index = GetRequest("page_index"); //当前查询的页码
	$per_page_count = GetRequest("per_page_count"); //每页行数
	$count = ($page_index - 1) * $per_page_count;

	//所有礼品默认 性别为0
	$str_sql = sprintf("select *, '%s' as filepath from t_store_%s where c_sub_type = '%s' limit %d,%d ",
	    $newPath, $tableName, EscapeString($type), $count, $per_page_count);

	DBHelper::QueryReturnJSON($str_sql);
}

// 获取某种礼品的一条记录
function HttpAction_GetBeautifulPresent()
{
	$id = $_REQUEST["id"];
	$tableName = "present";

	$newPath = GetBeautifulPath($tableName); //存放文件的文件夹路径
	$str_sql = sprintf("select *, '%s' as filepath from t_store_%s where c_id = %s",
	    $newPath, EscapeString($tableName), EscapeString($id));

	DBHelper::QueryReturnJSON($str_sql);
}
// 删除一条礼品记录
function HttpAction_DeleteBeautifulPresent()
{
	$id = $_REQUEST["id"];
	$tableName = "present";

	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);

	$msg = "";
	$rsArray = DBHelper::Query($str_sql, $msg);
	if ($rsArray == null) {
		ReturnJSONErrorMsg("错误：" . $msg);
		return;
	}
	if (count($rsArray) <= 0) {
		ReturnJSONErrorMsg("数据不存在...");
		return;
	}
	// 删除数据库
	$str_sql = sprintf("delete from t_store_%s where c_id = %s",
	    EscapeString($tableName),
	    EscapeString($id));

	if (DBHelper::ExecReturnJSON($str_sql)) {
		// 删除文件
		$gender = "0"; //礼品全默认性别为0
		$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件信息
		DeleteFileArray($fileInfoArray);
	}
}

// ------------------套装

// 判断套装是否上传连衣裙
function IsUploadSkirt()
{
	return isset($_REQUEST["upload_skirt"]);
}

// 判断套装是否上传上衣
function IsUploadCost()
{
	return isset($_REQUEST["upload_cost"]);
}

// 判断套装是否上传裤子
function IsUploadTrousers()
{
	return isset($_REQUEST["upload_trousers"]);
}
function HttpAction_UpdateDressPacket()
{
	CheckFileInfo("skirt_file_0");
	CheckFileInfo("skirt_file_1");

	CheckFileInfo("cost_file_0");
	CheckFileInfo("cost_file_1");

	CheckFileInfo("trousers_file_0");
	CheckFileInfo("trousers_file_1");

	$id = $_REQUEST["id"];
	$gender = $_REQUEST["gender"];
	$sub_type = $_REQUEST["sub_type"];
	$level = $_REQUEST["level"];

	$str_sql = sprintf("select * from t_dress_packet where c_id = '%s'", $id);

	$msg = "";
	$rsArray = DBHelper::Query($str_sql, $msg);
	if ($rsArray == - 1) {
		PrintMsgToHTML("错误：" . $msg);
		return;
	}

	// 连衣裙插入/修改
	$skirt_id = HttpAction_UpdateDressPacketSkirt($msg);
	if ($skirt_id == - 1) {
		PrintMsgToHTML("编辑连衣裙信息失败...:". $msg);
		return;
	}
	// 上衣插入/修改
	$cost_id = HttpAction_UpdateDressPacketCost($msg);
	if ($cost_id == - 1) {
		PrintMsgToHTML("编辑上衣信息失败...:". $msg);
		return;
	}
	// 裤子插入/修改
	$trousers_id = HttpAction_UpdateDressPacketTrousers($msg);
	if ($cost_id == - 1) {
		PrintMsgToHTML("编辑裤子信息失败...:". $msg);
		return;
	}

	if (count($rsArray) > 0) {
		$genderOld = $rsArray[0]["c_gender"];
		if ($gender != $genderOld) { // 修改了性别, 修改文件名
			EditBeautifulFileName("skirt", $skirt_id, $genderOld, $skirt_id, $gender);
			EditBeautifulFileName("cost", $cost_id, $genderOld, $cost_id, $gender);
			EditBeautifulFileName("trousers", $trousers_id, $genderOld, $trousers_id, $gender);
		}
		// 存在， 则修改
		$str_sql = sprintf("update t_dress_packet set c_gender='%s', c_sub_type='%s',
		c_level='%s', c_skirt_id='%s', c_cost_id='%s', c_trousers_id='%s' where c_id='%s'",
		EscapeString($gender),
		EscapeString($sub_type),
		EscapeString($level),
		EscapeString($skirt_id),
		EscapeString($cost_id),
		EscapeString($trousers_id),
		EscapeString($id)
		);
	} else {
		// 不存在， 则套装表插入
		$str_sql = sprintf("insert into t_dress_packet(c_gender, c_sub_type, c_level,
		c_skirt_id, c_cost_id, c_trousers_id) values('%s', '%s', '%s', '%s', '%s', '%s')",
		EscapeString($gender),
		EscapeString($sub_type),
		EscapeString($level),
		EscapeString($skirt_id),
		EscapeString($cost_id),
		EscapeString($trousers_id)
		);
	}
	DBHelper::ExecAndPrintMsg($str_sql);
}
// 失败返回 -1
function HttpAction_UpdateDressPacketSkirt(&$msg)
{
	$gender = $_REQUEST["gender"];
	$level = $_REQUEST["level"];
	$sub_type = GetRequest("sub_type");
	// 裤子信息
	$tableName = "skirt";
	$id = $_REQUEST["skirt_id"];

	$bIsUploadskirt = IsUploadSkirt(); //是否允许上传
	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);
	$bExistRow = DBHelper::QueryIsExistRow($str_sql);
	if (!$bExistRow && !$bIsUploadskirt)
	{
		//不存在 也不允许上传
		return 0;
	}
	if ($bExistRow && !$bIsUploadskirt) {
		// 已存在记录， 但不允许上传 ， 则删除原有的记录及文件
		if(!HttpAction_DeleteDressPacketskirt($id, $gender, $msg))
			$id = -1;
		else
			$id = "0";

		return $id;
	}

	$name = $_REQUEST["skirt_name"];
	$price_gold = $_REQUEST["skirt_price_gold"];
	$price_money = $_REQUEST["skirt_price_money"];

	$charm = $_REQUEST["skirt_charm"];
	$courage = $_REQUEST["skirt_courage"];
	$power = $_REQUEST["skirt_power"];
	$passion = $_REQUEST["skirt_passion"];

	$saleable = $_REQUEST["skirt_saleable"];
	$status = $_REQUEST["skirt_status"];
	$memo = $_REQUEST["skirt_memo"];

	if ($bExistRow && $bIsUploadskirt) {
		// 存在,并允许上传  则修改数据
		$str_sql = sprintf("update t_store_%s set c_saleable='%s',c_name='%s',
			c_price_gold='%s', c_price_money='%s', c_gender='%s', c_status='%s',
			c_memo='%s', c_sub_type='%s', c_charm='%s', c_courage='%s',c_power='%s',
			c_passion='%s', c_level='%s' where c_id='%s'",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level),
		EscapeString($id)
		);

		if (DBHelper::Exec($str_sql) == - 1)
			$id = - 1;
	}

	if (!$bExistRow && $bIsUploadskirt) {
		// 不存在记录， 但允许上传信息， 则 insert
		$str_sql = sprintf("insert into t_store_%s(c_saleable,c_name,
			c_price_gold,c_price_money,c_gender,c_status,
			c_memo,c_sub_type,c_charm,c_courage,c_power,c_passion,c_level)
			values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level)
		);

		$id = DBHelper::InsertReturnID($str_sql);
	}
	if ($id != - 1) {
		$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件路径信息
		UploadFile("skirt_file_0", $fileInfoArray[0]["path"], $fileInfoArray[0]["name"]);
		UploadFile("skirt_file_1", $fileInfoArray[1]["path"], $fileInfoArray[1]["name"]);
	}
	return $id;
}

// 失败返回 -1
function HttpAction_UpdateDressPacketCost(&$msg)
{
	$gender = $_REQUEST["gender"];
	$level = $_REQUEST["level"];
	$sub_type = GetRequest("sub_type");
	// 裤子信息
	$tableName = "cost";
	$id = $_REQUEST["cost_id"];

	$bIsUploadcost = IsUploadCost(); //是否允许上传
	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);
	$bExistRow = DBHelper::QueryIsExistRow($str_sql);
	if (!$bExistRow && !$bIsUploadcost)
	{
		//不存在 也不允许上传
		return 0;
	}
	if ($bExistRow && !$bIsUploadcost) {
		// 已存在记录， 但不允许上传 ， 则删除原有的记录及文件
		if(!HttpAction_DeleteDressPacketcost($id, $gender, $msg))
			$id = -1;
		else
			$id = "0";

		return $id;
	}

	$name = $_REQUEST["cost_name"];
	$price_gold = $_REQUEST["cost_price_gold"];
	$price_money = $_REQUEST["cost_price_money"];

	$charm = $_REQUEST["cost_charm"];
	$courage = $_REQUEST["cost_courage"];
	$power = $_REQUEST["cost_power"];
	$passion = $_REQUEST["cost_passion"];

	$saleable = $_REQUEST["cost_saleable"];
	$status = $_REQUEST["cost_status"];
	$memo = $_REQUEST["cost_memo"];

	if ($bExistRow && $bIsUploadcost) {
		// 存在,并允许上传  则修改数据
		$str_sql = sprintf("update t_store_%s set c_saleable='%s',c_name='%s',
			c_price_gold='%s', c_price_money='%s', c_gender='%s', c_status='%s',
			c_memo='%s', c_sub_type='%s', c_charm='%s', c_courage='%s',c_power='%s',
			c_passion='%s', c_level='%s' where c_id='%s'",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level),
		EscapeString($id)
		);

		if (DBHelper::Exec($str_sql) == - 1)
			$id = - 1;
	}

	if (!$bExistRow && $bIsUploadcost) {
		// 不存在记录， 但允许上传信息， 则 insert
		$str_sql = sprintf("insert into t_store_%s(c_saleable,c_name,
			c_price_gold,c_price_money,c_gender,c_status,
			c_memo,c_sub_type,c_charm,c_courage,c_power,c_passion,c_level)
			values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level)
		);

		$id = DBHelper::InsertReturnID($str_sql);
	}
	if ($id != - 1) {
		$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件路径信息
		UploadFile("cost_file_0", $fileInfoArray[0]["path"], $fileInfoArray[0]["name"]);
		UploadFile("cost_file_1", $fileInfoArray[1]["path"], $fileInfoArray[1]["name"]);
	}
	return $id;
}
// 失败返回 -1
function HttpAction_UpdateDressPacketTrousers(&$msg)
{
	$gender = $_REQUEST["gender"];
	$level = $_REQUEST["level"];
	$sub_type = GetRequest("sub_type");
	// 裤子信息
	$tableName = "trousers";
	$id = $_REQUEST["trousers_id"];

	$bIsUploadTrousers = IsUploadTrousers(); //是否允许上传裤子
	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);
	$bExistRow = DBHelper::QueryIsExistRow($str_sql);
	if (!$bExistRow && !$bIsUploadTrousers)
	{
		//不存在 也不允许上传
		return 0;
	}
	if ($bExistRow && !$bIsUploadTrousers) {
		// 已存在记录， 但不允许上传裤子信息， 则删除原有的记录及文件
		if(!HttpAction_DeleteDressPacketTrousers($id, $gender, $msg))
			$id = -1;
		else
			$id = "0";

		return $id;
	}

	$name = $_REQUEST["trousers_name"];
	$price_gold = $_REQUEST["trousers_price_gold"];
	$price_money = $_REQUEST["trousers_price_money"];

	$charm = $_REQUEST["trousers_charm"];
	$courage = $_REQUEST["trousers_courage"];
	$power = $_REQUEST["trousers_power"];
	$passion = $_REQUEST["trousers_passion"];

	$saleable = $_REQUEST["trousers_saleable"];
	$status = $_REQUEST["trousers_status"];
	$memo = $_REQUEST["trousers_memo"];

	if ($bExistRow && $bIsUploadTrousers) {
		// 存在,并上传裤子信息 则修改数据
		$str_sql = sprintf("update t_store_%s set c_saleable='%s',c_name='%s',
			c_price_gold='%s', c_price_money='%s', c_gender='%s', c_status='%s',
			c_memo='%s', c_sub_type='%s', c_charm='%s', c_courage='%s',c_power='%s',
			c_passion='%s', c_level='%s' where c_id='%s'",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level),
		EscapeString($id)
		);

		if (DBHelper::Exec($str_sql) == - 1)
			$id = - 1;
	}

	if (!$bExistRow && $bIsUploadTrousers) {
		// 不存在记录， 但允许上传裤子信息， 则 insert
		$str_sql = sprintf("insert into t_store_%s(c_saleable,c_name,
			c_price_gold,c_price_money,c_gender,c_status,
			c_memo,c_sub_type,c_charm,c_courage,c_power,c_passion,c_level)
			values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($gender),
		EscapeString($status),
		EscapeString($memo),
		EscapeString($sub_type),

		EscapeString($charm),
		EscapeString($courage),
		EscapeString($power),
		EscapeString($passion),
		EscapeString($level)
		);

		$id = DBHelper::InsertReturnID($str_sql);
	}
	if ($id != - 1) {
		$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件路径信息
		UploadFile("trousers_file_0", $fileInfoArray[0]["path"], $fileInfoArray[0]["name"]);
		UploadFile("trousers_file_1", $fileInfoArray[1]["path"], $fileInfoArray[1]["name"]);
	}
	return $id;
}

function HttpAction_GetDressPacketTotal()
{
	$gender = $_REQUEST["gender"];
	$str_sql = "select count(*) as total_count from t_dress_packet";

	// 2表示男女都查
	if ($gender != 2) {
		$str_sql = sprintf("%s where c_gender = '%s'", $str_sql, $gender);
	}

	DBHelper::QueryReturnJSON($str_sql);
}
function HttpAction_GetDressPacketList()
{
	$page_index = GetRequest("page_index"); //当前查询的页码
	$per_page_count = GetRequest("per_page_count"); //每页行数
	$count = ($page_index - 1) * $per_page_count;

	$gender = $_REQUEST["gender"];
	$str_sql = "select dp.c_id, dp.c_gender, dp.c_sub_type, dp.c_level, dp.c_glasses_id, dp.c_hair_id, dp.c_face_id"
	  . ", dp.c_hat_id, dp.c_cost_id, dp.c_trousers_id, dp.c_skirt_id, dp.c_shoes_id, dp.c_bag_id, dp.c_necklace_id, dp.c_socks_id"
	  . ", cost.c_name as cost_name, trousers.c_name as trousers_name, skirt.c_name as skirt_name"
	  . " from t_dress_packet dp"
	  . " left outer join t_store_cost cost on dp.c_cost_id = cost.c_id"
	  . " left outer join t_store_trousers trousers on dp.c_trousers_id = trousers.c_id"
	  . " left outer join t_store_skirt skirt on dp.c_skirt_id = skirt.c_id";

	// 2表示两者
	if ($gender != 2)
		$str_sql = sprintf("%s where dp.c_gender = '%s'", $str_sql, $gender);

	$str_sql = sprintf("%s limit %d,%d", $str_sql, $count, $per_page_count);

	DBHelper::QueryReturnJSON($str_sql);
}
function HttpAction_GetDressPacket()
{
	$id = $_REQUEST["id"];
	$str_sql = sprintf("select * from t_dress_packet where c_id = %s",
	    EscapeString($id));

	DBHelper::QueryReturnJSON($str_sql);
}

function HttpAction_DeleteDressPacket()
{
	$id = $_REQUEST["id"];
	$str_sql = sprintf("select * from t_dress_packet where c_id = '%s'", $id);
	$msg = "";
	$rsArray = DBHelper::Query($str_sql, $msg);
	if ($rsArray == -1)
	{
		ReturnJSONErrorMsg("错误：" . $msg);
		return;
	}
	if (count($rsArray) <= 0) {
		ReturnJSONErrorMsg("数据不存在...");
		return;
	}
	$gender = $rsArray[0]["c_gender"]; //性别信息

	$skirtID = $rsArray[0]["c_skirt_id"];
	$costID = $rsArray[0]["c_cost_id"];
	$trousersID = $rsArray[0]["c_trousers_id"];

	if(!HttpAction_DeleteDressPacketSkirt($skirtID, $gender, $msg))
	{
		ReturnJSONErrorMsg("删除连衣裙出错:". $msg);
		return;
	}
	if(!HttpAction_DeleteDressPacketCost($costID, $gender, $msg))
	{
		ReturnJSONErrorMsg("删除上衣出错:". $msg);
		return;
	}
	if(!HttpAction_DeleteDressPacketTrousers($trousersID, $gender, $msg))
	{
		ReturnJSONErrorMsg("删除裤子出错:". $msg);
		return;
	}

	$str_sql = sprintf("delete from t_dress_packet where c_id = %s", $id);
	DBHelper::ExecReturnJSON($str_sql);
}
function HttpAction_DeleteDressPacketSkirt($id, $gender, &$msg)
{
	$tableName = "skirt";

	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);
	if (DBHelper::QueryIsExistRow($str_sql)) {
		$str_sql = sprintf("delete from t_store_%s where c_id = %s",
		    EscapeString($tableName),
		    EscapeString($id));

		if (DBHelper::Exec($str_sql, $msg) == - 1)
			return false;
		// 删除文件
		$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件信息
		DeleteFileArray($fileInfoArray);
	}
	return true;
}
function HttpAction_DeleteDressPacketCost($id, $gender, &$msg)
{
	$tableName = "cost";

	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);
	if (DBHelper::QueryIsExistRow($str_sql)) {
		$str_sql = sprintf("delete from t_store_%s where c_id = %s",
		    EscapeString($tableName),
		    EscapeString($id));

		if (DBHelper::Exec($str_sql, $msg) == - 1)
			return false;
		// 删除文件
		$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件信息
		DeleteFileArray($fileInfoArray);
	}
	return true;
}
function HttpAction_DeleteDressPacketTrousers($id, $gender, &$msg)
{
	$tableName = "trousers";

	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);
	if (DBHelper::QueryIsExistRow($str_sql)) {
		$str_sql = sprintf("delete from t_store_%s where c_id = %s",
		    EscapeString($tableName),
		    EscapeString($id));

		if (DBHelper::Exec($str_sql, $msg) == - 1)
			return false;
		// 删除文件
		$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件信息
		DeleteFileArray($fileInfoArray);
	}
	return true;
}

// --------道具-----(存在，则update， 不存在，则insert)---
function HttpAction_UploadAmulet()
{
	CheckFileInfo("file_store");
	CheckFileInfo("file_deactive");
	CheckFileInfo("file_active");

	$type = $_REQUEST["type"];

	$saleable = $_REQUEST["saleable"];

	$price_gold = $_REQUEST["price_gold"];
	$price_money = $_REQUEST["price_money"];
	$validate_time = $_REQUEST["validate_time"];
	$memo = $_REQUEST["memo"];

	$str_sql = sprintf("select * from t_store_props where c_type = '%s'",
	    EscapeString($type));
	if (DBHelper::QueryIsExistRow($str_sql)) {
		// 存在 则修改数据
		$str_sql = sprintf("update t_store_props set c_type='%s',
		c_saleable='%s', c_price_gold='%s', c_price_money='%s',
		c_validate_time='%s', c_memo='%s' where c_type='%s'",
		EscapeString($type),
		EscapeString($saleable),
		EscapeString($price_gold),
		EscapeString($price_money),
		EscapeString($validate_time),
		EscapeString($memo),
		EscapeString($type)
		);
	} else {
		// 不存在， insert
		$str_sql = sprintf("insert into t_store_props
		(c_type, c_saleable, c_price_gold, c_price_money, c_validate_time, c_memo)
		values('%s', '%s', '%s', '%s', '%s', '%s')",
		EscapeString($type),
		EscapeString($saleable),
		EscapeString($price_gold),
		EscapeString($price_money),
		EscapeString($validate_time),
		EscapeString($memo)
		);
	}

	$msg = "";
	if(DBHelper::Exec($str_sql, $msg) == -1)
	{
		PrintMsgToHTML("编辑失败: " . $msg);
		return;
	}
	$fileInfoArray = GetAmuletFileInfo($type); //文件路径信息
	$str1 = UploadFile("file_store", $fileInfoArray[0]["path"], $fileInfoArray[0]["name"]);
	$str2 = UploadFile("file_deactive", $fileInfoArray[1]["path"], $fileInfoArray[1]["name"]);
	$str3 = UploadFile("file_active", $fileInfoArray[2]["path"], $fileInfoArray[2]["name"]);

	PrintMsgToHTML("编辑成功", $str1, $str2, $str3);

}
function HttpAction_GetAmulet()
{
	$type = $_REQUEST["type"];
	$newPath = GetAmuletPath($type);

	$str_sql = sprintf("select *, '%s' as filepath from t_store_props
		 where c_type = %s", $newPath, EscapeString($type));

	DBHelper::QueryReturnJSON($str_sql);
}
function HttpAction_DeleteAmulet()
{
	$type = $_REQUEST["type"];

	$str_sql = sprintf("select * from t_store_props where c_type = '%s'", $type);

	$msg = "";
	if (!DBHelper::QueryIsExistRow($str_sql, $msg))
	{
		ReturnJSONErrorMsg("数据不存在..." . $msg);
		return;
	}

	$str_sql = sprintf("delete from t_store_props where c_type = %s", $type);

	if(DBHelper::ExecReturnJSON($str_sql))
	{
		$fileInfoArray = GetAmuletFileInfo($type); //文件路径信息
		DeleteFileArray($fileInfoArray);
	}
}


//----- 家具上传
function HttpAction_UploadFurniture()
{
	CheckFileInfo("file_0");
	CheckFileInfo("file_1");

	$tableName = "furniture";
	$id = $_REQUEST["id"];
 
	$sub_type = GetRequest("sub_type");

	$name = $_REQUEST["name"];
	$price_gold = $_REQUEST["price_gold"];
	$price_money = $_REQUEST["price_money"];
 

	$saleable = $_REQUEST["saleable"]; 
	$memo = $_REQUEST["memo"];

	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);

	$bExec = true;
	$msg = "";
	$rsArray = DBHelper::Query($str_sql, $msg);
	if ($rsArray == - 1) {
		PrintMsgToHTML("错误：" . $msg);
		return;
	}

	if (count($rsArray) > 0) {
		// 存在 则修改数据
		$str_sql = sprintf("update t_store_%s set c_saleable='%s',c_name='%s',
		c_price_gold='%s', c_price_money='%s' ,
		c_memo='%s', c_type='%s'  where c_id='%s'",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($memo),
		EscapeString($sub_type),
		
		EscapeString($id)
		);

		if (DBHelper::Exec($str_sql, $msg) == - 1) {
			$bExec = false;
		}
	} else {
		// 不存在， insert
		$str_sql = sprintf("insert into t_store_%s(c_saleable,c_name, c_price_gold,c_price_money,	c_memo, c_type)
			values('%s', '%s', '%s', '%s', '%s', '%s')",
		$tableName,
		EscapeString($saleable),
		EscapeString($name),
		EscapeString($price_gold),
		EscapeString($price_money),

		EscapeString($memo),
		EscapeString($sub_type)
		);

		$id = DBHelper::InsertReturnID($str_sql, $msg);
		if ($id == - 1) {
			$bExec = false;
		}
	}

	if (!$bExec) {
		PrintMsgToHTML("编辑失败: " . $msg);
		return;
	}

	$gender = "0";  //家具的文件全默认性别为0
	$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件路径信息
	$finfo_0 = UploadFile("file_0", $fileInfoArray[0]["path"], $fileInfoArray[0]["name"]);
	$finfo_1 = UploadFile("file_1", $fileInfoArray[1]["path"], $fileInfoArray[1]["name"]);
   
	PrintMsgToHTML("编辑成功", $finfo_0, $finfo_1);
}

//--- 获取某种家具的总数(查询使用)
function HttpAction_GetBeautifulFurnitureTotal()
{
	$tableName = "furniture";
	$type = $_REQUEST["type"];
	$str_sql = sprintf("select count(*) as total_count from t_store_%s where c_type = '%s' ", $tableName, EscapeString($type));

	DBHelper::QueryReturnJSON($str_sql);
}
// 获取某种家具的所有记录(查询使用)
function HttpAction_GetBeautifulFurnitureList()
{
	$tableName = "furniture";
	$type = $_REQUEST["type"];

	$newPath = GetBeautifulPath($tableName); //存放文件的文件夹路径
	$page_index = GetRequest("page_index"); //当前查询的页码
	$per_page_count = GetRequest("per_page_count"); //每页行数
	$count = ($page_index - 1) * $per_page_count;

	//所有家具默认 性别为0
	$str_sql = sprintf("select *, '%s' as filepath, 0 as c_gender from t_store_%s where c_type = '%s' limit %d,%d ",
	    $newPath, $tableName, EscapeString($type), $count, $per_page_count);

	DBHelper::QueryReturnJSON($str_sql);
}

// 获取某种家具的一条记录
function HttpAction_GetBeautifulFurniture()
{
	$id = $_REQUEST["id"];
	$tableName = "furniture";

	$newPath = GetBeautifulPath($tableName); //存放文件的文件夹路径
	$str_sql = sprintf("select *, '%s' as filepath from t_store_%s where c_id = %s",
	    $newPath, EscapeString($tableName), EscapeString($id));

	DBHelper::QueryReturnJSON($str_sql);
}
// 删除一条家具记录
function HttpAction_DeleteBeautifulFurniture()
{
	$id = $_REQUEST["id"];
	$tableName = "furniture";

	$str_sql = sprintf("select * from t_store_%s where c_id = '%s'", $tableName, $id);

	$msg = "";
	$rsArray = DBHelper::Query($str_sql, $msg);
	if ($rsArray == null) {
		ReturnJSONErrorMsg("错误：" . $msg);
		return;
	}
	if (count($rsArray) <= 0) {
		ReturnJSONErrorMsg("数据不存在...");
		return;
	}
	// 删除数据库
	$str_sql = sprintf("delete from t_store_%s where c_id = %s",
	    EscapeString($tableName),
	    EscapeString($id));

	if (DBHelper::ExecReturnJSON($str_sql)) {
		// 删除文件
		$gender = "0"; //家具全默认性别为0
		$fileInfoArray = GetBeautifulFileInfo($tableName, $id, $gender); //文件信息
		DeleteFileArray($fileInfoArray);
	}
}

// 问题------------------------
function HttpAction_UpdateQuestion()
{
	$id = $_REQUEST["id"];
	$type = $_REQUEST["type"];
	$content = $_REQUEST["content"];
	$group = $_REQUEST["group"];
	$msg = "";

	$str_sql = sprintf("select * from t_question where c_id = '%s'", $id);
	if (DBHelper::QueryIsExistRow($str_sql, $msg)) {
		// 存在 则修改数据
		$str_sql = sprintf("update t_question set c_type='%s', c_content='%s',
			c_group='%s' where c_id='%s'",
		EscapeString($type),
		EscapeString($content),
		EscapeString($group),
		EscapeString($id));

		if(DBHelper::Exec($str_sql, $msg)== -1)
		{
			$id = -1;
			PrintMsgToHTML("编辑出错: ". $msg);
			return;
		}
		// 删除选项表
		$str_sql = sprintf("delete from t_question_option where c_question_id='%s'",
		    EscapeString($id));

		if(DBHelper::Exec($str_sql, $msg)== -1)
			$id = -1;
	} else
	{
		// 不存在则插入插入主表
		$str_sql = sprintf("insert into t_question(c_type, c_content, c_group) values('%s', '%s', '%s')",
		    EscapeString($type), EscapeString($content), EscapeString($group));

		$id = DBHelper::InsertReturnID($str_sql, $msg);
	}

	if($id == -1)
	{
		PrintMsgToHTML("编辑出错: ". $msg);
		return;
	}
	// 插入选项表
	for($i = 0; $i < 10; $i++) {
		$tempName = "option_" . $i;
		if (isset($_REQUEST[$tempName]) && strlen ($_REQUEST[$tempName]) > 0) {
			$str_sql = sprintf("insert into t_question_option(c_question_id, c_content)
				values(%d, '%s')", $id, EscapeString($_REQUEST[$tempName]));
			DBHelper::Exec($str_sql, $msg);
		}
	}

	PrintMsgToHTML("编辑成功");
}
function HttpAction_GetQuestionTotal()
{
	$type = GetRequest("type");
	$str_sql = sprintf("select count(*) as total_count from t_question where c_type='%s'",
	    EscapeString($type));
	DBHelper::QueryReturnJSON($str_sql);
}
function HttpAction_GetQuestionList()
{
	$type = $_REQUEST["type"];

	$page_index = GetRequest("page_index"); //当前查询的页码
	$per_page_count = GetRequest("per_page_count"); //每页行数
	$count = ($page_index - 1) * $per_page_count;

	$str_sql = sprintf("select * from t_question where c_type = %s limit %d,%d",
	    EscapeString($type),
	    $count,
	    $per_page_count);
	DBHelper::QueryReturnJSON($str_sql);
}

function HttpAction_GetQuestion()
{
	$id = $_REQUEST["id"];
	$str_sql = sprintf("select * from t_question where c_id = '%s'",
	    EscapeString($id));
	DBHelper::QueryReturnJSON($str_sql);
}
function HttpAction_GetQuestionOption()
{
	$id = $_REQUEST["id"];
	$str_sql = sprintf("select * from t_question_option where c_question_id = '%s'",
	    EscapeString($id));
	DBHelper::QueryReturnJSON($str_sql);
}
function HttpAction_DeleteQuestion()
{
	$id = $_REQUEST["id"];

	$str_sql = sprintf("select * from t_question where c_id = '%s'", $id);
	if (!DBHelper::QueryIsExistRow($str_sql))
	{
		ReturnJSONMsg("数据不存在...");
		return;
	}

	$str_sql = sprintf("delete from t_question_option where c_question_id = '%s'", $id);
	DBHelper::Exec($str_sql);//删除选项表

	$str_sql = sprintf("delete from t_question where c_id = '%s'", $id);
	DBHelper::ExecReturnJSON($str_sql);
}
// 星座信息------------------------------
function HttpAction_GetConstellationDescript()
{
	$index_0 = $_REQUEST["index"];

	$str_sql = sprintf("select * from t_constellation where c_index='%s' ",
	    EscapeString($index_0));
	DBHelper::QueryReturnJSON($str_sql);
}
function HttpAction_UpdateConstellationDescript()
{
	$index = $_REQUEST["index"];
	$descript = $_REQUEST["descript"];

	$name = $_REQUEST["name"];
	$start = "2000-" . $_REQUEST["start"];
	$end = "2000-" . $_REQUEST["end"];

	$str_sql = sprintf("select * from t_constellation  where c_index='%s'",
	    EscapeString($index));
	// 不存在则插入，存在 则修改
	if (!DBHelper::QueryIsExistRow($str_sql)) {
		// 不存在星座信息， 则insert
		$str_sql = sprintf("insert into t_constellation(c_index, c_name,
		c_start, c_end, c_description)
		values('%s', '%s', '%s', '%s', '%s')",
		EscapeString($index),
		EscapeString($name),
		EscapeString($start),
		EscapeString($end),
		EscapeString($descript)
		);
	} else {
		$str_sql = sprintf("update t_constellation set c_name='%s',
		c_start='%s', c_end='%s', c_description='%s' where c_index='%s'",
		EscapeString($name),
		EscapeString($start),
		EscapeString($end),
		EscapeString($descript),
		EscapeString($index));
	}

	DBHelper::ExecAndPrintMsg($str_sql);
}
// 星座匹配------------------------------
function HttpAction_GetConstellationPairDescript()
{
	$index_0 = $_REQUEST["index_0"];
	$index_1 = $_REQUEST["index_1"];

	$str_sql = sprintf("select * from t_constellation_pair where c_first='%s' and c_second='%s' ",
	    EscapeString($index_0),
	    EscapeString($index_1)
	    );
	DBHelper::QueryReturnJSON($str_sql);
}
function HttpAction_UpdateConstellationPairDescript()
{
	$index_0 = $_REQUEST["index_0"];
	$index_1 = $_REQUEST["index_1"];
	$level = $_REQUEST["level"];
	$time = $_REQUEST["time"];
	$descript = $_REQUEST["descript"];

	$str_sql = sprintf("select * from t_constellation_pair where c_first='%s' and c_second='%s' ",
	    EscapeString($index_0),
	    EscapeString($index_1)
	    );
	// 不存在则插入，存在 则修改
	if (!DBHelper::QueryIsExistRow($str_sql)) {
		// 不存在星座信息， 则insert
		$str_sql = sprintf("insert into t_constellation_pair(c_first, c_second, c_level, c_time, c_description)
			values('%s', '%s', '%s', '%s', '%s')",
		EscapeString($index_0),
		EscapeString($index_1),
		EscapeString($level),
		EscapeString($time),
		EscapeString($descript)
		);
	} else {
		$str_sql = sprintf("update t_constellation_pair set c_first='%s', c_second='%s', c_level='%s',
			c_time='%s', c_description='%s' where c_first='%s' and c_second='%s'",
		EscapeString($index_0),
		EscapeString($index_1),
		EscapeString($level),
		EscapeString($time),
		EscapeString($descript),

		EscapeString($index_0),
		EscapeString($index_1)
		);
	}

	DBHelper::ExecAndPrintMsg($str_sql);
}
// ---------查询类别信息-----------------
function HttpAction_GetGlassesSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["glasses_sub_type"], "", "");
}
function HttpAction_GetHatSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["hat_sub_type"], "", "");
}
function HttpAction_GetHairSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["hat_sub_type"], "", "");
}
function HttpAction_GetFaceSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["face_sub_type"], "", "");
}
function HttpAction_GetSkinSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["skin_sub_type"], "", "");
}
function HttpAction_GetCostSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["cost_sub_type"], "", "");
}
function HttpAction_GetTrousersSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["trousers_sub_type"], "", "");
}
function HttpAction_GetSkirtSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["skirt_sub_type"], "", "");
}
function HttpAction_GetBagSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["bag_sub_type"], "", "");
}
function HttpAction_GetNecklaceSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["necklace_sub_type"], "", "");
}
function HttpAction_GetEarringSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["earring_sub_type"], "", "");
}
function HttpAction_GetBraceletSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["bracelet_sub_type"], "", "");
}
function HttpAction_GetWingSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["wing_sub_type"], "", "");
}

function HttpAction_GetTattooSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["tattoo_sub_type"], "", "");
}
function HttpAction_GetBarwaiterSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["barwaiter_sub_type"], "", "");
}
function HttpAction_GetMusicSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["music_sub_type"], "", "");
}
function HttpAction_GetShoesSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["shoes_sub_type"], "", "");
}
function HttpAction_GetSocksSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["socks_sub_type"], "", "");
}
function HttpAction_GetDressPacketSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["dress_packet_sub_type"], "", "");
}
function HttpAction_GetPresentSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["present_sub_type"], "", "");
}
function HttpAction_GetFurnitureSubTypeList()
{
	ReturnJSONFromArray($GLOBALS["furniture_sub_type"], "", "");
}
function HttpAction_GetJobsAwardTypeList()
{
	ReturnJSONFromArray($GLOBALS["jobs_award_type"], "", "");
}
function HttpAction_GetQuestionGroupList()
{
	ReturnJSONFromArray($GLOBALS["question_group"], "", "");
}

?>